# !/usr/bin/python3
# -*- coding: utf-8 -*-

"""
岗位信息表控制器类
----------------------------------------------------
@Project :   xinhou-openai-framework
@File    :   Post.py
@Contact :   sp_hrz@qq.com

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/04/16 22:04  peng.shen   v1.0.0     None
"""

from fastapi import APIRouter, Depends
from fastapi.params import Path
from loguru import logger
from sqlalchemy.orm import Session

from apps.admin.schema.PostSchema import ReqPostFindSchema, ReqPostSaveSchema, ReqPostUpdateSchema
from apps.admin.schema.RoleSchema import ResRoleDetailSchema
from common.entity.Post import Post
from common.service.PostService import PostService
from xinhou_openai_framework.core.db.DatabaseManager import DatabaseManager
from xinhou_openai_framework.core.exception.GlobalExceptionType import ParameterException
from xinhou_openai_framework.core.reponse.R import R
from xinhou_openai_framework.core.reponse.ResModel import ResModel
from xinhou_openai_framework.pages.PageHelper import PageResultHelper, PageHelper

api = APIRouter()


@api.post('/admin/post/find',
          tags=["admin", "post"],
          response_model=ResModel[PageResultHelper[ResRoleDetailSchema]],
          summary="查询岗位信息表信息接口",
          description="通过参数模型传递条件查询")
async def find(search: PageHelper[ReqPostFindSchema], db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[index][request]:{}".format(search.model_dump()))
    return R.SUCCESS(PostService(db).find_by(search))


@api.post('/admin/post/findAll',
          tags=["admin", "post"],
          response_model=ResModel[PageResultHelper[ResRoleDetailSchema]],
          summary="查询岗位信息表信息接口",
          description="通过参数模型传递条件查询")
async def find_all(search: ReqPostFindSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[index][request]:{}".format(search.model_dump()))
    return R.SUCCESS(PostService(db).find_all(search))


@api.post("/admin/post/save",
          tags=["admin", "post"],
          response_model=ResModel[ResRoleDetailSchema],
          summary="保存岗位信息表信息接口",
          description="通过参数模型保存数据")
async def save(model: ReqPostSaveSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[save][request]:{}".format(model.model_dump_json()))
    return R.SUCCESS(PostService(db).save(Post(**model.model_dump(exclude_unset=True))))


@api.get("/admin/post/delete/{id}",
         tags=["admin", "post"],
         response_model=ResModel[bool],
         summary="删除岗位信息表信息接口",
         description="根据ID删除数据")
async def delete(id: int = Path(title="ID不能为空"), db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[detail][request]:{}".format({"id": id}))
    if id is None:
        raise ParameterException()
    return R.SUCCESS(PostService(db).delete(Post(id=id)))


@api.post("/admin/post/update",
          tags=["admin", "post"],
          response_model=ResModel[ResRoleDetailSchema],
          summary="更新岗位信息表信息接口",
          description="根据ID更新数据")
async def update(model: ReqPostUpdateSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[save][request]:{}".format(model.model_dump_json()))
    if model.id is None:
        raise ParameterException()
    return R.SUCCESS(PostService(db).update(Post(**model.model_dump(exclude_unset=True))))


@api.get("/admin/post/detail/{id}",
         tags=["admin", "post"],
         response_model=ResModel[ResRoleDetailSchema],
         summary="获取岗位信息表详情接口",
         description="根据ID获取数据")
async def detail(id: int = Path(title="ID不能为空"), db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[detail][request]:{}".format({"id": id}))
    if id is None:
        raise ParameterException()
    return R.SUCCESS(PostService(db).find_by_id(Post(id=id)))
